package cn.jxwazx.sheinspringboot.service.impl;


import cn.jxwazx.sheinspringboot.entity.param.UserLoginParam;
import cn.jxwazx.sheinspringboot.entity.po.UserInfo;
import cn.jxwazx.sheinspringboot.entity.vo.UserPageVo;
import cn.jxwazx.sheinspringboot.exception.MyException;
import cn.jxwazx.sheinspringboot.mapper.UserInfoMapper;
import cn.jxwazx.sheinspringboot.service.UserInfoService;
import cn.jxwazx.sheinspringboot.utils.CheckUtil;
import cn.jxwazx.sheinspringboot.utils.JwtUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.hutool.core.bean.BeanUtil;
import org.dromara.hutool.core.map.MapUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;

/**
 * @author LoveHuaHua
 * @date 2023年11月11日 22:49
 * @description believe in yourself
 */
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor(onConstructor = @__(@Lazy))
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {


    /**
     * 登录
     *
     * @param userInfo
     * @return {@link String}
     */
    @Override
    public String login(UserLoginParam userInfo) {

        boolean exists = lambdaQuery().eq(UserInfo::getUsername, userInfo.getAccount()).exists();
        CheckUtil.checkTrue(exists, new MyException("用户不存在！"));
        UserInfo one = lambdaQuery().eq(UserInfo::getUsername, userInfo.getAccount()).one();
        CheckUtil.checkTrue(one.getPassword().equals(userInfo.getPassword()), new MyException("密码错误!"));
        HashMap<String, Object> map = MapUtil.newHashMap();
        map.put("id", one.getId());
        return JwtUtil.encrypt(map);
    }



    /**
     * 获取用户详情
     *
     * @param id
     * @return {@link UserPageVo}
     */
    @Override
    public UserPageVo detail(Long id) {
        UserPageVo userPageVo = new UserPageVo();

        UserInfo userInfo = getById(id);
        BeanUtil.copyProperties(userInfo, userPageVo);
        return userPageVo;
    }
}
